package com.chenjt.four_aiservicedemo;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.agent.tool.ToolSpecifications;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;

import java.time.LocalDateTime;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2025/3/8 16:07
 */
public class UserAIService {



    interface Assistant {
        /**
         * 返回值类型在dev.langchain4j.service.TypeUtils中查看
         * 1.进入AiServices.build()这个方法的实现方法
         * 2.找到TypeUtils.validateReturnTypesAreProperlyParametrized
         * 3.在注释上去看有什么返回值，对应的接口就可以用什么返回值
         * @param userId 聊天窗口id
         * @param message 用户输入信息
         * @return
         */
        String chat(@MemoryId Long userId, @UserMessage String message);
    }


    @Tool("获取当前日期")
    public static String dateUtil(){
        return LocalDateTime.now().toString();
    }

   public static void main(String[] args) throws NoSuchMethodException {

       MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);


       ChatLanguageModel model = OpenAiChatModel.builder()
               .apiKey("demo")
               .modelName("gpt-4o-mini")
               .build();

       ToolSpecification toolSpecification = ToolSpecifications.toolSpecificationFrom(UserAIService.class.getMethod("dateUtil"));

       Assistant assistant = AiServices.builder(Assistant.class)
               .chatLanguageModel(model)
//               .chatMemory(chatMemory)
               // 通过memoryId去找到chatMemory
               .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
               .tools(toolSpecification)
               .build();


       System.out.println(assistant.chat(1L,"你好，我是小明"));
       System.out.println(assistant.chat(1L,"我的名字是什么"));

       System.out.println(assistant.chat(2L, "你好，我是小强"));
       System.out.println(assistant.chat(2L, "我的名字是什么"));


   }



}
